package vooga.xml.handlers;


/**
 * This class models a value that be shared, incremented, reset,
 * and compared to other COunters.
 *
 * @author Robert C. Duvall
 */
public class Counter implements Comparable
{
    //////////////////////////////////////////////////////////////
    // State
    private int myInitialValue;
    private int myCurrentValue;


    //////////////////////////////////////////////////////////////
    // Constructors
    /**
     * Create Counter that starts at 0
     */
    public Counter ()
    {
        this(0);
    }

    /**
     * Create Counter that starts at initialValue
     */
    public Counter (int initialValue)
    {
        myInitialValue = initialValue;
        myCurrentValue = initialValue;
    }



    //////////////////////////////////////////////////////////////
    // Accessors
    /**
     * Return the Counter's current value.
     */
    public int getValue ()
    {
        return myCurrentValue;
    }

    /**
     * Return the Counter's starting value.
     */
    public int getInitialValue ()
    {
        return myInitialValue;
    }

    /**
     * Returns a negative integer, zero, or a positive integer 
     * if this Counter is less than, equal to, or greater than o.
     */
    public int compareTo (Object o)
    {
        Counter other = (Counter)o;
        if (other != null)
        {
            return getValue() - other.getValue();
        }
        return 0;
    }

    /**
     * Return a string representing the Counter, for printing.
     */
    public String toString ()
    {
        return "" + getValue();
        // OR, more properly:
        // return new Integer(getValue()).toString();
    }


    //////////////////////////////////////////////////////////////
    // Mutators
    /**
     * Increment the current value by 1.
     */
    public void increment ()
    {
        update(1);
    }

    /**
     * Decrement the current value by 1.
     */
    public void decrement ()
    {
        update(-1);
    }

    /**
     * Change the current value by amount.
     */
    public void update (int amount)
    {
        myCurrentValue += amount;
    }

    /**
     * Reset the Counter's current value to its initial value.
     */
    public void reset ()
    {
        myCurrentValue = getInitialValue();
    }
}
